<template>
  <div class="recource-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form
          :inline="true"
          :model="formInline"
          class="demo-form-inline"
          ref="form"
        >
          <el-form-item prop="name" label="资源名称">
            <el-input v-model="formInline.name"></el-input>
          </el-form-item>
          <el-form-item prop="url" label="资源路径">
            <el-input v-model="formInline.url"></el-input>
          </el-form-item>
          <el-form-item prop="categoryId" label="资源分类">
            <el-select
              v-model="formInline.categoryId"
              placeholder="请选择资源分类"
              clearable="true"
            >
              <el-option
                :label="item.name"
                :value="item.id"
                :key="item.id"
                v-for="item in resourceCatory"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" :disabled="isLoading"
              >查询</el-button
            >
            <el-button @click="onReset" :disabled="isLoading">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="resources"
        style="width: 100%; margin-bottom: 24px"
        v-loading="isLoading"
      >
        <el-table-column type="index" label="编号"> </el-table-column>
        <el-table-column prop="name" label="资源名称"> </el-table-column>
        <el-table-column prop="url" label="资源路径"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="createdTime" label="添加时间"> </el-table-column>
        <el-table-column label="操作" min-width="120px">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
            <el-button size="mini" type="primary"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="formInline.current"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="formInline.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        :disabled="isLoading"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import { getResourcePages } from "@/services/resource";
import { getAllCategory } from "@/services/resource-category";
import { Form } from "element-ui";

export default Vue.extend({
  name: "ResourceList",
  data() {
    return {
      formInline: {
        name: "",
        url: "",
        current: 1,
        size: 8,
        categoryId: null,
      },
      resources: [],
      totalCount: 0,
      resourceCatory: [],
      isLoading: false,
    };
  },
  created() {
    this.loadResource();
    this.loadResourceCategory();
  },
  methods: {
    onSubmit() {
      this.formInline.current = 1;
      this.loadResource();
    },
    onReset() {
      // this.formInline.name = "";
      // this.formInline.url = "";
      // this.formInline.current = 1;
      // this.formInline.size = 8;
      // this.formInline.categoryId = null;
      (this.$refs.form as Form).resetFields();
      this.loadResource();
    },
    async loadResource() {
      this.isLoading = true;
      const { data } = await getResourcePages(this.formInline);
      this.resources = data.data.records;
      this.totalCount = data.data.total;
      this.isLoading = false;
    },
    async loadResourceCategory() {
      const { data } = await getAllCategory();
      this.resourceCatory = data.data;
    },
    handleEdit(item: unknown) {
      console.log(item);
    },
    handleDelete(item: unknown) {
      console.log(item);
    },
    // handleAdd(item: unknown) {},
    handleSizeChange(val: number) {
      this.formInline.size = val;
      this.formInline.current = 1;
      this.loadResource();
    },
    handleCurrentChange(val: number) {
      this.formInline.current = val;
      this.loadResource();
    },
  },
});
</script>

<style lang="scss" scoped>
</style>